<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//会计记账管理
class TeamCash extends CommonController {

	function __construct(){
		
		parent::__construct();
		$this->load->model('Accounting/TeamAccountingModel','CurModel');
		$this->load->model('Accounting/AccountingBalanceModel','BalanceModel');
	}
	
	function share_save(){
		$user_id = $this->user_id;
		if(!$this->check_priv('accounting_duizhang_2')){
			$this->err_out(LANG_NOPRIV);
		}

		$date_month = $this->input->post('date_month');
		$list = $this->input->post('list',null,array());
		$xdata = $this->input->post('xdata',null,array());
		$mtype = $this->input->post('mtype',null,0);

		//分摊数据,去掉前面2行和最后一行
		$list = array_slice($list,2,-1);

		//重组xdata
		$n_xdata = array();
		foreach($xdata as $k=>$v){
			$n_xdata['bumen'.$k] = $v['name'];
		}
		
		$subject_arr = $list_data = array();
		$num = count($n_xdata) - 1;
		foreach($list as $item){
			$subject_arr[] = $item['name'];

			if($item['bz'] == '人民币'){
				$bs = 1;
			}else{
				$this->load->model('User/CurrencyModel','CurrencyModel');
				$curr = $this->CurrencyModel->bz_get_info($item['bz']);
				$bs = $curr?$curr['exchange_rate']:1;
			}
			for($i=0;$i<=$num;$i++){
				$list_data[] = array(
					'user_id' => $user_id,
					'company' => '',
					'date' => $date_month.'-1',
					'date_month' => $date_month,
					'region' => '',
					'bumen' => $n_xdata['bumen'.$i],
					'bumen_scale' => 100,
					'income' => $mtype?'':toFloat($item['bumen'.$i] / $bs),
					'expenses' => $mtype?toFloat($item['bumen'.$i] / $bs):'',
					'info' => '由'.$date_month.'月份的'.$item['name'].'分摊得来',
					'subject' => $item['name'],
					'certificate' => '',
					'type' => 2,
					'mtype' => $mtype,
					'money' => $item['bumen'.$i],
					'bz' => $item['bz'],
					'addtime' => time(),
					'balance' => 0,
					'status' => 1
				);
			}
			
		}

		//先将该月份的这些科目删除，然后再添加
		$this->CurModel->del_to_insert($subject_arr,$list_data);

		$this->data_out(1);
	}

	//start
	/**
	* @name 获取列表
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_list(){
		$user_id = $this->user_id;
		
		
		if(!$this->check_priv('accounting_duizhang|accounting_duizhang_1')){
			$this->err_out(LANG_NOPRIV);
		}


		$page = $this->input->post('page',null,1);
		$size = $this->input->post('size',null,10);
		
		$filter = $this->input->post('f',null,array());

		
		if(empty($filter['start_date_month'])){
			$this->err_out('请选择月份');
		}
		else{
			$filter['start'] = date('Y-m-d',strtotime($filter['start_date_month'].'-01'));
		}

		//截止日期
		if(!empty($filter['end_date_month'])){
			$filter['end'] = date('Y-m-d',strtotime($filter['end_date_month'].'-01 +1 month -1 day'));
		}else{
			$filter['end'] = date('Y-m-d');
		}
		

		$info = array();
		
		$info['balance_status'] = 0;
		$current_month = date('Y-m',strtotime($filter['date_month'].'-01'));
		$is_exist = $this->BalanceModel->_where_exist(array('date_month'=>$current_month,'status'=>0));
		if($is_exist){
			$info['balance_status'] = 1;
		}
		
		
		$total_before = 0;
		

		$arr = $filter;
		unset($arr['date_month']);
		$cash_res = $this->CurModel->get_list($arr,$page,$size);
		
		$cash_list = $cash_res['list'];
		$info['count'] = $cash_res['count'];
		$this->load->model('User/UserModel','UserModel');
		$this->load->model('User/SubjectModel','SubjectModel');//为了取出科目码, ling添加
		
		foreach($cash_list as $key=>$val){
			$cash_list[$key]['month'] = date('m',strtotime($val['date']));
			$cash_list[$key]['day'] = date('d',strtotime($val['date']));
			

			

			
			if($val['mtype']==0){
				$cash_list[$key]['income_s'] = $val['money'];
				$cash_list[$key]['expenses_s'] = '-';
			}
			else{
				$cash_list[$key]['income_s'] = '-';
				$cash_list[$key]['expenses_s'] = $val['money'];
			}
			
			$cash_list[$key]['username'] = '未知';
			$ls_user = $this->UserModel->get_user($val['user_id']);
			if($ls_user){
				$cash_list[$key]['username'] = $ls_user['nickname'];
			}
			
			//通过科目名称取科目码,ling添加
			$subject_name = $this->SubjectModel->get_subject($val['subject']);
			if($subject_name){
				//$cash_list[$key]['subject'] = "(".$subject_name['code'].")".$cash_list[$key]['subject'];
				$cash_list[$key]['subject_code'] = $subject_name['code'];
			}
			
		}
		$info['list'] = $cash_list;
		

		
		$this->data_out($info);
	}

	//start
	/**
	* @name 引入记账
	* @param date ture char 日期开始和结束
	* @return 成功返回1
	*/
	//end
	function yinru(){
		
		if(!$this->check_priv('accounting_duizhang_2')){
			$this->err_out(LANG_NOPRIV);
		}
		
		
		$filter = $this->input->post('f',null,array());
		if(empty($filter['date'])){
			$this->err_out('请选择日期范围');
		}
		
		$fg = explode('|',$filter['date']);
		if(count($fg)<2){
			$this->err_out('错误的日期时间');
		}
		$start = $fg[0];
		$end = $fg[1];
		
		$farr = array('type','region','company','bumen','subject');
		$arr = array();
		foreach($filter as $key=>$val){
			if(in_array($key,$farr)){
				$arr[$key] = $val;
			}
		}
		
		$arr['start'] = $start;
		$arr['end'] = $end;
		
		
		$result = $this->CurModel->yinru_data($arr);
		if(!$result){
			$this->err_out('引入数据错误');
		}
		
		$this->data_out(1);
		
	}
}
